package com.smartthinking.destinomusical.adapter;

//Need the following import to get access to the app resources, since this
//class is in a sub-package.
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

import com.smartthinking.destinomusical.R;
import com.smartthinking.destinomusical.db.ControlBD;
import com.smartthinking.destinomusical.obj.Cancion;

public class PlaylistAdapter extends BaseAdapter implements Filterable{
	private LayoutInflater inflater;
	private Activity a;
	
	private ControlBD bd;
	private Context mContext;
	List<?> mdataObjectList;
	private List<Cancion> all = new ArrayList<Cancion>();
	
	public PlaylistAdapter(Activity a, List<?> dataObjectList) {
		this.a = a;
		bd = new ControlBD(a);
		mContext = a.getApplicationContext();
		mdataObjectList = dataObjectList;
		for (Object c : mdataObjectList) {
			all.add((Cancion) c);
		}
		
		inflater = (LayoutInflater) a
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return mdataObjectList == null ? 0 : mdataObjectList.size();
	}

	@Override
	public Object getItem(int position) {
		return mdataObjectList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final Cancion obj = (Cancion) mdataObjectList.get(position);
		View vi = convertView;
		ViewHolder holder;
		if (vi == null) {
			vi = inflater.inflate(R.layout.listitem_playlist, null);
			holder = new ViewHolder();
			holder.title = (TextView) vi.findViewById(R.id.title);
			holder.desc = (TextView) vi.findViewById(R.id.desc);
			holder.eliminar = (ImageView) vi.findViewById(R.id.eliminar);
			vi.setTag(holder);
		} else
			holder = (ViewHolder) vi.getTag();
		holder.title.setText(obj.getNombre());
		holder.desc.setText("(" + obj.getCodigo() + ") " + obj.getArtista());
		holder.eliminar.setTag(obj.getId());
		holder.eliminar.setId(position);
		holder.eliminar.setOnClickListener(delete);
		return vi;
	}

	

	OnClickListener delete = new OnClickListener() {
		@Override
		public void onClick(View v) {
			// System.out.println("Deleting object");
			ControlBD bd = new ControlBD(mContext);
			bd.deleteCancion("(" + v.getTag() + ")");
			mdataObjectList.remove(v.getId());
			notifyDataSetChanged();
		}
	};

	public static class ViewHolder {
		public TextView title;
		public TextView desc;
		public ImageView eliminar;

	}
	
	@Override
	public Filter getFilter() {
		Filter myFilter = new Filter() {
			@Override
			protected FilterResults performFiltering(CharSequence constraint) {
				List<Cancion> filtered = new ArrayList<Cancion>();
				filtered.clear();
				if (constraint == null || constraint.equals("")||constraint.equals(null)) {
					filtered = all;
					//filtered.count = all.size();

				} else {
					for (int i = 0; i < all.size(); i++) {
						if (all.get(i).getNombre()
								.toUpperCase()
								.startsWith(constraint.toString().toUpperCase())) {
							filtered.add(all.get(i));
						}
					}
					;
				}
				FilterResults filterResults = new FilterResults();
				filterResults.values = filtered;
				filterResults.count = filtered.size();
				return filterResults;
			}

			@Override
			protected void publishResults(CharSequence contraint,
					FilterResults results) {
				if (results != null && results.count > 0) {
					mdataObjectList=(List<Cancion>) results.values;
					notifyDataSetChanged();
				} else {
					notifyDataSetInvalidated();
				}
			}
		};
		return myFilter;
	}
	
	

}